﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WssUtils;

/// <summary>
/// 获取网格图案和相对坐标位置的组件
/// </summary>
public class UIGridPattern : MonoBehaviour
{
    public string pattern = "111,000,111";
    public int angle = 0;
    public float gridSize = 70;

    private Vector2[] patternPos = { };
#if UNITY_EDITOR
    private void OnValidate()
    {
        this.angle = (this.angle % 360 / 90) * 90;
        LoadFromData(pattern, this.angle);
    }

#endif

    public void LoadFromData(string pattern, int angle)
    {
        this.pattern = pattern;
        patternPos = GetArrayPos(pattern, angle);

        int count = patternPos.Length;
        int curCount = this.transform.childCount;
        if (curCount < count)
        {
            for (int i = 0; i < count - curCount; i++)
            {
                var child = Instantiate(this.transform.GetChild(0));
                child.transform.SetParent(this.transform);
                child.transform.localScale = Vector3.one;
            }

        }

        UpdateDisplay();

    }


    public void UpdateDisplay()
    {
        int index = 0;
        this.transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);

        foreach (Transform child in this.transform)
        {
            if (index < patternPos.Length)
            {
                child.gameObject.SetActive(true);
                var pos = patternPos[index];
                child.transform.localPosition = pos * gridSize;
            }
            else
            {
                child.gameObject.SetActive(false);
            }
            index++;

        }

    }

    public static Vector2[] GetArrayPos(string pattern, int angle = 0)
    {
        var temp = new List<Vector2>();
        var patterns = GetArrayData(pattern, angle);
        var h = patterns.Length;
        var w = patterns[0].Length;
        Vector2 pivot = new Vector2(w / 2.0f - 0.5f, -(h / 2.0f - 0.5f));

        for (int y = 0; y < patterns.Length; y++)
        {
            for (int x = 0; x < patterns[y].Length; x++)
            {
                if (patterns[y][x] > 0)
                {
                    var pos = new Vector2(x, -y) - pivot;
                    temp.Add(pos);
                }

            }

        }
        return temp.ToArray();
    }

    /// <summary>
    /// 获取棋牌数据
    /// </summary>
    /// <param name="pattern"></param>
    /// <param name="angle"></param>
    /// <returns></returns>
    public static int[][] GetArrayData(string pattern, int angle = 0)
    {

        int[][] arr = ConvertToIntArray2D(pattern);

        if (angle != 0 && arr != null)
        {
            return ArrayTool.Rotate2D(arr, angle);
        }
        else
        {
            return arr;
        }
    }

    /// <summary>
    /// 将pattern 数据转换为数组(适用于一种符号的情况)
    /// </summary>
    /// <param name="pattern"></param>
    /// <returns></returns>
    private static int[][] ConvertToIntArray2D(string pattern)
    {
        char[] arr = pattern.ToCharArray();
        var all = new List<int[]>();
        var temp = new List<int>();
        int slot = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] != ',')
            {
                int.TryParse(arr[i].ToString(), out slot);
                temp.Add(slot);
            }

            if (arr[i] == ',' || i == arr.Length - 1)
            {
                all.Add(temp.ToArray());
                temp.Clear();
            }
        }

        return all.ToArray();
    }

    /// <summary>
    /// 将pattern 数据转换为字母数组 (使用于多种符号的情况)
    /// </summary>
    /// <param name="pattern"></param>
    /// <returns></returns>
    private static char[][] ConvertToCharArray2D(string pattern)
    {
        char[] arr = pattern.ToCharArray();
        var all = new List<char[]>();
        var temp = new List<char>();
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] != ',')
            {
                temp.Add(arr[i]);
            }

            if (arr[i] == ',' || i == arr.Length - 1)
            {
                all.Add(temp.ToArray());
                temp.Clear();
            }
        }
        return all.ToArray();
    }

}
